<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-25
 * Time: 下午12:29
 */
class UsersinvoiceModel extends BaseModel{
    private $_table;
    protected $_name = 'users_invoice';
    protected $_isbug = false;

    public function __construct() {
        parent::__construct();
    }

    /**
     * 根据用户的id查询用户公司信息
     * @param $id
     * @return array
     */
    public function getUserInfoById($id,$is_default,$type){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $select = $db->select ();

        $sql = $select->from ($this->_name,array("id as business_id","business_name","contacts","mobile","business_phone","is_default","quotation_update_time","certificate"))
            ->where ("user_id = '{$id}'")
            ->__toString ();

        if(!empty($is_default)){
            $sql .= " and is_default = '{$is_default}'";
        }

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        if($type=='a'){
           $data =  $db->fetchAll( $sql );
        }
        if($type=='r'){
            $data =  $db->fetchRow( $sql );
        }
        return $data;
    }

    /**
     * @param $name
     * @return array
     */
    public function getUserInfoByName($name){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $select = $db->select ();

        $sql = $select->from ($this->_name,array("id as business_id","business_name","contacts","mobile","business_phone","is_default"))
            ->where ("business_name = '{$name}'")
            ->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data =  $db->fetchRow( $sql );

        return $data;
    }

    /**
     * 根据公司的id获取公司信息
     * @param $id
     * @return array
     */
    public function getBusinessInfoById($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $select = $db->select ();

        $sql = $select->from ($this->_name,array("id as business_id","business_name","contacts","mobile","business_phone","is_default"))
            ->where ("id = '{$id}'")
            ->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data =  $db->fetchRow( $sql );

        return $data;
    }

    /**
     * @param $where
     * @return array
     */
    public function getUserBusinessyName($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $select = $db->select ();

        $sql = $select->from ($this->_name,array("id as business_id","business_name","contacts","mobile","business_phone","is_default","certificate"))
            ->where ($where)
            ->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data =  $db->fetchRow( $sql );

        return $data;
    }

    /**
     * @param $data
     * @param $where
     * @return int
     */
    public function updateBusinessByWhere($data,$where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        return $db->update($this->_name,$data,$where);
    }


}